﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMS.Core.Services;
using AMS.Core.Data;
using System.Runtime.Serialization;
using AMS.DD35.DbLoader.DD35Service;
using System.ServiceModel;
using AMS.Core;

namespace AMS.DD35.DbLoader.DataAccess
{
    public class LoginProxy : ILoginService
    {
        //private ILoginService _loginService;
        private DD35MasterServiceClient _dd35service;

        ISessionService ILoginService.SessionService
        {
            get { throw new NotImplementedException(); }
        }

        public LoginProxy()
        {
            RestartConnection();
        }

        public object AttemptLogin(LoginData loginData)
        {
            try
            {
                return _dd35service.AttemptLogin(loginData);
            }
            catch (FaultException)
            {
                RestartConnection();
                ExceptionHandler.Exception("login failed");
            }
            // consider use of FaultException<T> in order to not break connection?

            return null;
        }

        public void Logout(object sessionVariable)
        {
            try
            {
                _dd35service.Logout(sessionVariable);
            }
            catch (FaultException)
            {
                //RestartConnection();
                //ExceptionHandler.Exception("logout failed");
            }
        }

        private void RestartConnection()
        {
            _dd35service = new DD35MasterServiceClient("WS2007HttpBinding_IDD35MasterService");
        }
    }
}
